package com.cneport.transfer.router.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cneport.transfer.router.model.Node;
import com.cneport.transfer.router.support.service.NodeService;
import com.cneport.transfer.util.IdGenerate;

/**
 * Copyright (c) 2011, 东方口岸科技有限公司 All rights reserved. 数据字典维护层
 * @author zhangqiang2
 * @version 1.0
 * @since 2011-1-10
 */
@Controller
@RequestMapping("/node")
public class NodeController {

    /** dicService 数据字典服务层 */
    @Autowired
    private NodeService nodeService;
    /** */
    @SuppressWarnings("unused")
    private static final Log LOG = LogFactory.getLog(NodeController.class);

    /**
     * 根据typeId获取数据字典值(下拉框使用）
     * @param request
     *            客户端的处理请求
     * @param response
     *            返回处理后的结果
     * @return Map 结果集
     */
    @RequestMapping(value = "/getNodeAllByTypeId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getDicAllByTypeId(HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> responseMap = new HashMap<String, Object>();
        Map<String, Object> paramMap = new HashMap<String, Object>();

        String inputValue = request.getParameter("inputValue");
        // 判断输入的值
        inputValue = inputValue == null ? " " : inputValue;
        inputValue = "%" + inputValue + "%";
        System.out.println(inputValue);

        responseMap.put("data", nodeService.getNodeAllByTypeId(inputValue));
        return responseMap;

    }

    /**
     * 查询节点信息
     * @param request
     *            请求
     * @param response
     *            响应
     * @return 2012-7-18 作 者：wudi
     */
    @RequestMapping(value = "/queryNodeList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryNodeList(HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> responseMap = new HashMap<String, Object>();

        try {
            String status = request.getParameter("status");
            String nodeId = request.getParameter("nodeId");
            String start = request.getParameter("start");
            String limit = request.getParameter("limit");

            Node node = new Node();
            node.setStatus(status);
            node.setNodeId(nodeId);
            node.setStart(Integer.parseInt(start));
            node
                    .setLimit(Integer.parseInt(start) + Integer.parseInt(limit)
                            + 1);

            List<Node> reslut = nodeService.getNodeListService(node);
            int totalProperty = nodeService.getNodeTotalCount(node);

            responseMap.put("success", true);
            responseMap.put("root", reslut);
            responseMap.put("totalProperty", totalProperty);
        } catch (Exception e) {
            responseMap.put("success", false);
            LOG.error(e);
        }

        return responseMap;
    }

    /**
     * 删除节点
     * @param request
     *            请求
     * @param response
     *            响应
     * @return 2012-7-18 作 者：wudi
     */
    @RequestMapping(value = "/deleteNode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteNode(HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> responseMap = new HashMap<String, Object>();

        try {
            String nodeId = request.getParameter("nodeId");
            nodeService.deleteNodeService(nodeId);
            responseMap.put("success", true);
        } catch (Exception e) {
            responseMap.put("success", false);
            LOG.error(e);
        }

        return responseMap;
    }

    /**
     * 获取节点信息
     * @param request
     *            请求
     * @param response
     *            响应
     * @return 2012-7-18 作 者：wudi
     */
    @RequestMapping(value = "/getNode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getNode(HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> responseMap = new HashMap<String, Object>();

        try {
            String nodeId = request.getParameter("nodeId");
            Node node = nodeService.getNode(nodeId);

            responseMap.put("data", node);
            responseMap.put("success", true);
        } catch (Exception e) {
            responseMap.put("success", false);
            LOG.error(e);
        }

        return responseMap;
    }

    /**
     * 保存节点
     * @param request
     *            请求
     * @param response
     *            响应
     * @return 2012-7-18 作 者：wudi
     */
    @RequestMapping(value = "/saveNode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateNode(HttpServletRequest request,
            HttpServletResponse response) {
        Map<String, Object> responseMap = new HashMap<String, Object>();

        try {
            String flag = request.getParameter("flag");
            System.out.println(flag);
            String nodeId = request.getParameter("nodeId");
            String nodeName = request.getParameter("nodeName");
            String parentId = request.getParameter("parentId");
            String nodeIP = request.getParameter("nodeIP");
            String transType = request.getParameter("transType");
            String transAddr = request.getParameter("transAddr");
            String remark = request.getParameter("remark");
            String status = request.getParameter("status");

            Node node = new Node();
            node.setNodeId(nodeId);
            node.setNodeName(nodeName);
            node.setParentId(parentId);
            node.setNodeIP(nodeIP);
            node.setTransType(transType);
            node.setTransAddr(transAddr);
            node.setRemark(remark);
            node.setStatus(status);
            node.setOperTime(new Date());

            if ("edit".equals(flag)) {
                nodeService.updateNodeService(node);
            } else if ("add".equals(flag)) {
                node.setId(IdGenerate.getUUIDString());
                nodeService.insertNodeService(node);
            }
            responseMap.put("success", true);
        } catch (Exception e) {
            responseMap.put("success", false);
            LOG.error(e);
        }

        return responseMap;
    }

}
